import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)

const city = window.localStorage.getItem('city')
const userInfo = window.localStorage.getItem('userInfo')
const token = window.localStorage.getItem('token')

const store = new Vuex.Store({
  state: {
    // 标题
    title: '影片列表',
    // 当前选择的城市
    city: city ? JSON.parse(city) : null,
    // 用户信息
    userInfo: userInfo ? JSON.parse(userInfo) : null,
    // token
    token: token || null
  },

  getters: {
    cityId (state) {
      return state.city.cityId
    }
  },

  mutations: {
    SET_TITLE (state, title) {
      state.title = title
    },

    SET_CITY (state, city) {
      state.city = city
      window.localStorage.setItem('city', JSON.stringify(city))
    },

    SET_USER_INFO_AND_TOKEN (state, payload) {
      state.userInfo = payload.userInfo
      state.token = payload.token

      window.localStorage.setItem('userInfo', JSON.stringify(payload.userInfo))
      window.localStorage.setItem('token', payload.token)
    }
  }
})

export default store
